package cn.devzyh.oms.wsms.controller;

import cn.devzyh.oms.common.core.util.R;
import cn.devzyh.oms.common.excel.annotation.ResponseExcel;
import cn.devzyh.oms.common.mybatis.domain.PageQuery;
import cn.devzyh.oms.common.security.annotation.Inner;
import cn.devzyh.oms.wsms.api.dto.StockDTO;
import cn.devzyh.oms.wsms.api.entity.Stock;
import cn.devzyh.oms.wsms.api.vo.StockVO;
import cn.devzyh.oms.wsms.service.StockService;
import cn.hutool.core.collection.CollectionUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 库存记录
 *
 * @author devzyh
 * @date 2023-10-29
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/stock")
public class StockController {

	private final StockService stockService;

	/**
	 * 分页查询
	 *
	 * @param page  分页参数
	 * @param stock 查询条件
	 * @return R
	 */
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('wsms_stock_view')")
	public R getStockPage(@ModelAttribute PageQuery<Stock> page, @ModelAttribute Stock stock) {
		return R.ok(stockService.list(page.mybatisPlusPage(), stock));
	}

	/**
	 * 通过ID查询
	 *
	 * @param id id
	 * @return R
	 */
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('wsms_stock_view')")
	public R getById(@PathVariable("id") Long id) {
		Stock s = new Stock();
		s.setId(id);
		List<StockVO> stocks = stockService.list(null, s);
		return R.ok(CollectionUtil.isEmpty(stocks) ? null : stocks.get(0));
	}

	/**
	 * 导出Excel表格
	 *
	 * @param stock 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('wsms_stock_export')")
	public List<StockVO> export(@ModelAttribute Stock stock) {
		return stockService.list(null, stock);
	}

	/**
	 * 库存调整
	 *
	 * @param dto 库存操作数据
	 * @return R
	 */
	@Inner
	@PostMapping(value = {"/adjust"})
	public R adjust(@RequestBody StockDTO dto) {
		return stockService.adjust(dto);
	}

	/**
	 * 批量库存调整
	 *
	 * @param list     库存操作列表
	 * @param rollback 发生错误时是否回滚
	 * @return R
	 */
	@Inner
	@PostMapping(value = {"/batch_adjust"})
	public R batchAdjust(@RequestBody List<StockDTO> list, @RequestParam boolean rollback) {
		return stockService.batchAdjust(list, rollback);
	}

	/**
	 * 库存在途
	 *
	 * @param dto 库存操作数据
	 * @return R
	 */
	@Inner
	@PostMapping(value = {"/in_transit"})
	public R inTransit(@RequestBody StockDTO dto) {
		return stockService.inTransit(dto);
	}

	/**
	 * 库存入库
	 *
	 * @param dto 库存操作数据
	 * @return R
	 */
	@Inner
	@PostMapping(value = {"/inbound"})
	public R inbound(@RequestBody StockDTO dto) {
		return stockService.inbound(dto);
	}

	/**
	 * 库存分配
	 *
	 * @param dto 库存操作数据
	 * @return R
	 */
	@Inner
	@PostMapping(value = {"/allocate"})
	public R allocate(@RequestBody StockDTO dto) {
		return stockService.allocate(dto);
	}

	/**
	 * 库存出库
	 *
	 * @param dto 库存操作数据
	 * @return R
	 */
	@Inner
	@PostMapping(value = {"/outbound"})
	public R outbound(@RequestBody StockDTO dto) {
		return stockService.outbound(dto);
	}

}